﻿#include "ManualRefundOrChangeDialog.h"
#include <algorithm>
extern AllData* allData;

ManualRefundOrChangeDialog::ManualRefundOrChangeDialog(QWidget* parent)
    : QDialog(parent) {
    setWindowTitle("手工退改");
    resize(700, 400);
    QVBoxLayout* layout = new QVBoxLayout(this);
    table = new QTableWidget(this);
    table->setColumnCount(6);
    table->setHorizontalHeaderLabels({"订单ID", "用户ID", "车次ID", "座位ID", "状态", "操作"});
    layout->addWidget(table);
    QPushButton* refundBtn = new QPushButton("退票", this);
    QPushButton* changeBtn = new QPushButton("改签", this);
    layout->addWidget(refundBtn);
    layout->addWidget(changeBtn);
    connect(refundBtn, &QPushButton::clicked, this, &ManualRefundOrChangeDialog::onRefund);
    connect(changeBtn, &QPushButton::clicked, this, &ManualRefundOrChangeDialog::onChange);
    loadAbnormalOrders();
}

void ManualRefundOrChangeDialog::onRefund() {
    int row = table->currentRow();
    if (row < 0) {
        QMessageBox::warning(this, "提示", "请先选择订单");
        return;
    }
    int ret = QMessageBox::question(this, "确认退票", "确定要退票吗？", QMessageBox::Yes | QMessageBox::No);
    if (ret != QMessageBox::Yes) return;
    int orderId = table->item(row, 0)->text().toInt();
    auto it = std::find_if(allData->ordersQueue.begin(), allData->ordersQueue.end(),
        [orderId](orders& o) { return o.getOrderId() == orderId; });
    if (it == allData->ordersQueue.end()) {
        QMessageBox::warning(this, "提示", "订单不存在");
        return;
    }
    // 判断座位占用信息是否还在
    auto seatIt = std::find_if(allData->seatOccupyQueue.begin(), allData->seatOccupyQueue.end(),
        [orderId](const SeatOccupy& so) { return so.getOrderId() == orderId; });
    if (seatIt == allData->seatOccupyQueue.end()) {
        QMessageBox::information(this, "提示", "之前已经成功退改，无需再退改");
        return;
    }
    // 释放座位占用记录
    auto& queue = allData->seatOccupyQueue;
    queue.erase(std::remove_if(queue.begin(), queue.end(), [&](const SeatOccupy& so) {
        return so.getOrderId() == it->getOrderId();
    }), queue.end());
    allData->SeatOccupy_WriteToFile("seat_occupy.txt");
    QMessageBox::information(this, "成功", "退票成功");
    loadAbnormalOrders();
}

void ManualRefundOrChangeDialog::onChange() {
    int row = table->currentRow();
    if (row < 0) {
        QMessageBox::warning(this, "提示", "请先选择订单");
        return;
    }
    int orderId = table->item(row, 0)->text().toInt();
    auto it = std::find_if(allData->ordersQueue.begin(), allData->ordersQueue.end(),
        [orderId](orders& o) { return o.getOrderId() == orderId; });
    if (it == allData->ordersQueue.end()) {
        QMessageBox::warning(this, "提示", "订单不存在");
        return;
    }
    // 判断座位占用信息是否还在
    auto seatIt = std::find_if(allData->seatOccupyQueue.begin(), allData->seatOccupyQueue.end(),
        [orderId](const SeatOccupy& so) { return so.getOrderId() == orderId; });
    if (seatIt == allData->seatOccupyQueue.end()) {
        QMessageBox::information(this, "提示", "之前已经成功退改，无需再退改");
        return;
    }
    // 释放原座位占用记录
    auto& queue = allData->seatOccupyQueue;
    queue.erase(std::remove_if(queue.begin(), queue.end(), [&](const SeatOccupy& so) {
        return so.getOrderId() == it->getOrderId();
    }), queue.end());
    allData->SeatOccupy_WriteToFile("seat_occupy.txt");
    QMessageBox::information(this, "成功", "改签成功");
    loadAbnormalOrders();
}

void ManualRefundOrChangeDialog::loadAbnormalOrders() {
    table->setRowCount(0);
    int row = 0;
    for (const auto& o : allData->ordersQueue) {
        if (o.getStatus() != orders::booked) {
            table->insertRow(row);
            table->setItem(row, 0, new QTableWidgetItem(QString::number(o.getOrderId())));
            table->setItem(row, 1, new QTableWidgetItem(QString::number(o.getUserId())));
            table->setItem(row, 2, new QTableWidgetItem(QString::number(o.getTrainId())));
            table->setItem(row, 3, new QTableWidgetItem(QString::number(o.getSeatId())));
            QString statusStr;
            switch (o.getStatus()) {
                case orders::booked: statusStr = "已预订"; break;
                case orders::canceled: statusStr = "已退票"; break;
                case orders::changed: statusStr = "已改签"; break;
                default: statusStr = "异常"; break;
            }
            table->setItem(row, 4, new QTableWidgetItem(statusStr));
            table->setItem(row, 5, new QTableWidgetItem(""));
            row++;
        }
    }
}
